package com.ktjy.ktmall.servlet;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ktjy.ktmall.entity.GoodsItem;
import com.ktjy.ktmall.entity.SkuItem;
import com.ktjy.ktmall.service.CartItemService;
import com.ktjy.ktmall.service.GoodsItemService;
import com.ktjy.ktmall.service.impl.CartItemServiceImpl;
import com.ktjy.ktmall.service.impl.GoodsItemServiceImpl;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@WebServlet(name = "AddProductServlet",urlPatterns = "/uni/addProduct")
public class AddProductServlet extends HttpServlet {


    private GoodsItemService goodsItemService = null;
    @Override
    public void init(ServletConfig config) throws ServletException {
        goodsItemService = new GoodsItemServiceImpl();
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String params = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
        JSONObject entries = JSONUtil.parseObj(params);
        GoodsItem goodsItem = new GoodsItem();
        goodsItem.setGoodsName(entries.getStr("goodsName"));
        goodsItem.setSpuCode(entries.getStr("spuCode"));


        List<Object> skuLists= entries.getJSONArray("skuList");

        List<SkuItem> skuItemList = new ArrayList<>();

        goodsItemService.addProduct(goodsItem);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
